/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cersoft.xmascharity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.*;
import javax.annotation.*;
import javax.faces.bean.*;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

/**
 *
 * @author Marcin
 */
@javax.faces.bean.ManagedBean
@RequestScoped
public class PageManager {

    private List<Page> pages;
    private String activePageId;
    private String path;

    @PostConstruct
    public void init() {

        FacesContext context = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
        path = servletContext.getRealPath("/resources/content");

        pages = new ArrayList<Page>();

        File d = new File(path);
        for (File f : d.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".html");
            }
        })) {
            String sId = null;
            String sTitle = null;
            try {
                sId = f.getName().substring(0, f.getName().indexOf("."));
                sTitle = sId.substring(1 + sId.indexOf("-"));
                sId = sId.substring(0, sId.indexOf("-"));
            } catch (Exception e) {
                servletContext.log(e.getMessage());
            }
            if (sId != null && !sId.isEmpty() && sTitle != null && !sTitle.isEmpty()) {
                Page p = new Page();
                p.setPid(sId);
                p.setTitle(sTitle);
                StringBuilder content = new StringBuilder();
                try {
                    BufferedReader br = new BufferedReader(new FileReader(f));
                    String line;
                    while ((line = br.readLine()) != null) {
                        content.append(line).append("\n");
                    }
                } catch (Exception e) {
                    servletContext.log(e.getMessage());
                }
                System.out.println(content);
//                p.setContent(content.toString());
                  p.setContent("./resources/content/"+f.getName());
                pages.add(p);
            }
        }
    }

    public String getActivePageId() {
        return activePageId;
    }

    public void setActivePageId(String activePageId) {
        System.out.println("setting active " + activePageId);
        this.activePageId = activePageId;
    }

    public Page getActivePage() {
        System.out.println("getting active: " + this.activePageId);
        for (Page p : this.pages) {
            if (p.getPid().equals(this.activePageId)) {
                return p;
            }
        }
        return this.pages.get(0);
    }

    public List<Page> getPages() {
        return pages;
    }

    public void setPages(List<Page> pages) {
        this.pages = pages;
    }
}
